<template>
  <div class="light-green lighten-5 pt-5">
    <my-feed 
      :feed="message" 
      @queryFeed="queryUserFeed(uid)" 
      v-for="message in messages" 
      :key="message.forwardId +'#'+ message.feedId">
    </my-feed>
    <el-empty v-show="messages.length == 0" description="该用户暂无动态"></el-empty>
  </div>
</template>

<script>
import MyFeed from '../views/MyFeed.vue'

export default {
  name: 'MyUserFeed',
  components: {MyFeed},
  data: ()=>({
    messages:[],
    uid: '',
  }),
  computed: {
    user(){
      return this.$store.state.user
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      const uid = to.query.id;
      // 根据路由id，查询并设置用户页的信息
      vm.queryUserFeed(uid)
      vm.uid = uid
    })
  },
  methods:{
    doQueryUserFeed(url, config){
      this.$utils.doGetAxios(this, url, config, data=>{
        
        this.messages = data.map((item,)=>{
          if(item.forwardId){
            item.forwardTime = this.$utils.dateFormat(item.forwardTime)
            item.time = this.$moment(item.time).format("YYYY-MM-DD HH:mm:ss")
          }else{
            item.time = this.$utils.dateFormat(item.time)
          }

          item.showComment = false,
          item.sendContent = '',
          item.comments = []
          return item
        })
      })
    },
    queryUserFeed(uid){
      const userId = this.user.userId || localStorage.getItem('userId') 
      const token = this.user.token || localStorage.getItem('token') 

      if(userId && token){
        this.doQueryUserFeed('/api/user/feed/u', {
          params:{userId, uid},
          headers: {token }
        })
      }else{
        this.doQueryUserFeed('/api/user/feed', {params: {userId: uid}})
      }
    }
  }
}
</script>

<style scoped>

</style>